package com.my.helper.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.my.helper.R;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.MyDataBaseHelper;
import com.my.helper.util.SharedPrefUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建备忘
 */
public class CrateBieWangActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout rlBack;
    private EditText etInputContent;
    private String strInputContent;
    private TextView tvContent;
    private TimePickerView pvData;
    private RelativeLayout rlDate;
    private TextView tvDate;
    private Button btnCrate;
    private String strContent;
    private String strTime;
    private MyDataBaseHelper mSql;
    private int userId;
    private SharedPrefUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crate_bie_wang);
        CacheActivity.addActivity(this);
        sp = new SharedPrefUtil(this, "myHelper");

        initView();
        initListener();
        initData();
    }

    private void initView() {
        rlBack = findViewById(R.id.rl_back_bei_wang);
        rlDate = findViewById(R.id.rl_date_cbw);
        btnCrate = findViewById(R.id.btn_crate_cbw);

        etInputContent = findViewById(R.id.et_cbw);
        tvContent = findViewById(R.id.tv_input_text_cbw);
        tvDate = findViewById(R.id.tv_date_cbw);

        pvData = new TimePickerView(this,
                TimePickerView.Type.ALL);

        pvData.setTime(new Date());
        pvData.setCyclic(false);
        pvData.setCancelable(true);
    }

    private void initData() {
        mSql = new MyDataBaseHelper(this, "Helper.db", null, 1);
    }

    private void initListener() {
        rlBack.setOnClickListener(this);
        rlDate.setOnClickListener(this);
        btnCrate.setOnClickListener(this);

        etInputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int contentSize = 0;
                strInputContent = etInputContent.getText().toString();
                contentSize = strInputContent.length();
                tvContent.setText("已输入" + contentSize + "/100个字");

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        // 选择日期后的回调
        pvData.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                strTime = getTime(date);
                tvDate.setText(strTime);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back_bei_wang: // 返回
                CacheActivity.finishSingleActivityByClass(CrateBieWangActivity.class);
                break;
            case R.id.rl_date_cbw: // 选择日期
                pvData.show();
                break;
            case R.id.btn_crate_cbw: // 提交
                crateBeiWang();
                break;
        }
    }

    /**
     * 创建备忘
     */
    private void crateBeiWang() {
        strContent = etInputContent.getText().toString();
        if (strContent.isEmpty()) {
            ToastUtil("请输入备忘内容");
        } else if (strTime == null) {
            ToastUtil("请选择日期");
        } else {
            userId = Integer.valueOf(sp.getString("user_id", "0"));
            Log.i("mwb", "CrateBieWangActivity userId:" + userId);
            SQLiteDatabase db = mSql.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("uid", userId);
            values.put("content", strContent);
            values.put("date", strTime);
            db.insert("MyMemorandum", null, values);
            values.clear();
            db.close();

            ToastUtil("创建成功");
            CacheActivity.finishSingleActivityByClass(CrateBieWangActivity.class);
        }
    }

    private void ToastUtil(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

}
